package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @className ValidateCodeController
 */
@RestController
@RequestMapping("/validatecode")
@RequiredArgsConstructor
@Slf4j
public class ValidateCodeController {

    private final RedisTemplate<String, Integer> redisTemplate;

    /**
     * 体检预约发送验证码
     * @param telephone 手机号
     * @return Result
     **/
    @PostMapping("/send4Order")
    public Result sendValidateCodeByOrder(String telephone) {
        //生成验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        log.info("随机生成的验证码：{}",validateCode);
        //缓存验证码
        redisTemplate.opsForValue().set("Order:" + telephone, validateCode,5, TimeUnit.MINUTES);
        //发送验证码，阿里云不对个人开放，测试直接使用随机验证码生成
        // SMSUtils.sendMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 快速登录发送验证码
     * @param telephone 手机号
     * @return Result
     **/
    @GetMapping("/send4Login")
    public Result sendValidateCodeByLogin(String telephone){
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        log.info("随机生成的验证码：{}",validateCode);
        redisTemplate.opsForValue().set("Login:" + telephone, validateCode,5, TimeUnit.MINUTES);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
